﻿$(function() {
    /**
	 * 设备能耗
	 * */
    var ratepie = echarts.init(document.getElementById('ratepie'));
    optionpie = {
        tooltip: {
            trigger: 'item',
            formatter: function(params, ticket, callback) {
                if (params.seriesName == "设备状态") {
                    return params.seriesName + " <br/>" + params.name + " : " + params.value + "小时 " + "(" + params.percent + "%)";
                } else {
                    return params.seriesName + " <br/>" + params.name + " : " + params.value + "度 " + "(" + params.percent + "%)";
                }
            }
        },
        legend: {
            orient: 'vertical',
            left: 'center',
            top: 'middle',
            data: []
        },
        backgroundColor: '#e0e0e0',
        color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
        series: [{
            name: '设备状态',
            type: 'pie',
            radius: '60%',
            center: ['25%', '45%'],
            data: [],
            label: {
                normal: {
                    show: false,
                    formatter: '{b}\n{c}分钟'
                }
            }
        },
        {
            name: '能耗状态',
            type: 'pie',
            radius: '60%',
            center: ['75%', '45%'],
            data: [],
            label: {
                normal: {
                    show: false,
                    formatter: '{b}\n{c}度'
                }
            }
        }]
    };
    ratepie.setOption(optionpie);

    /**
	 * 状态时长
	 * */
    var ratebar1 = echarts.init(document.getElementById('ratebar1'));
    optionbar1 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            top: '10%',
            left: '4%',
            right: '4%',
            bottom: '20%',
            containLabel: true
        },
        legend: {
            bottom: '30',
            data: []
        },
        backgroundColor: '#e0e0e0',
        color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
        xAxis: {
            type: 'category',
            data: [],
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            name: '状态时长(分钟)',
            axisLabel: {
                formatter: '{value}'
            },
            splitLine: {
                show: false
            }
        },
        dataZoom: {
            type: 'slider',
            start: 0,
            end: 100,
            bottom: 1
        },
        series: []
    };
    ratebar1.setOption(optionbar1);

    /**
     * 满载率
     * */

    var ratebar2 = echarts.init(document.getElementById('ratebar2'));
    optionBar2 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        backgroundColor: '#e0e0e0',
        color:['#f7aa47'],
        grid: {
            top: '10%',
            left: '4%',
            right: '4%',
            bottom: '20%',
            containLabel: true
        },
        legend: {
            bottom: '30',
            data: ['满载率']
        },
        xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
            nameLocation: 'end',
            axisLabel: {
                formatter: '{value}'
            },
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'category',
            name: '满载率',
            data: [],
            //['早班满载率','晚班满载率','全天满载率'],
            axisLabel: {
                formatter: '{value}'
            },
            splitLine: {
                show: false
            }
        },
        series: [{
            name: '满载率',
            type: 'bar',
            label: {
                normal: {
                    show: true,
                    formatter: '{c}%',
                    position: 'right'
                }
            },
            data: []
        }]
    };
    ratebar2.setOption(optionBar2);

    /**
     * 初始化
     * */
    var vm = new Vue({
        el: '#itemData',
        data: {
            item: []
        }
    });
    var lineId = 0;
    var equId = 0;
    $('#ss').slider('setValue', parseInt(moment().format("x")));
    var getsliderTime = $('#ss').slider('getValue');
    var time = moment(getsliderTime).format("YYYY-MM-DD");
    getData();
    function getData() {
        $.post(basePath + "/single/singleDay/startrate.json", {
            "lineId": lineId,
            "equId": equId,
            belongDate: time
        },
        function(res) {
            vm.$set('item', res);
            if (res.success) {
                ratepie.setOption({
                    legend: {
                        data: res.stateTypes
                    },
                    series: [{
                        name: '设备状态',
                        data: res.pieSValue
                    },
                    {
                        name: '能耗状态',
                        data: res.pieDValue
                    }]
                });
                var series = [];
                for (var i = 0; i < res.yValue.length; i++) {
                    series.push({
                        name: res.yValue[i].name,
                        stack: '总量',
                        type: 'bar',
                        barMaxWidth: 40,
                        data: res.yValue[i].value
                    });
                }
                ratebar1.setOption({
                    legend: {
                        data: res.stateTypes
                    },
                    xAxis: {
                        data: res.xTimes
                    },
                    series: series
                });

                ratebar2.setOption({
                    yAxis: {
                        data: res.shiftList
                    },
                    series: [{
                        name: '满载率',
                        data: res.rates
                    }]
                });
            } else {
                alert(res.msg);
            }
        });
    }

    /**
     * slider滑动事件
     * */
    $('#ss').slider({
        onComplete: function(value) { //滑块值改变时触发
            time = moment(value).format('YYYY-MM-DD');
            getData();
        }
    });

    /**
     * 自定义时间按钮的点击事件
     * */
    $("#defineTime").click(function() {
        var mePanel = $('#defineTimeWin');
        mePanel.dialog('open');
    });

    /**
	  * 切换设备的点击事件
	  * */
    var mePanel = $('#switchEquip');
    $("#linetree").tree({
        onClick: function(node) {
            if (node.url) {
                lineId = node.id;
                var lineGrid = $('#lines_data');
                lineGrid.datagrid('load', {
                    lineId: node.id
                });
                lineGrid.datagrid('getPanel').panel('setTitle', node.text);
            }
        }
    });
    $('#lines_data').datagrid({
        //双击设备时，根据选中设备刷新
        onDblClickRow: function(rowIndex, rowData) {
            lineId = rowData.lineId;
            equId = rowData.id;
            time = moment($('#ss').slider('getValue')).format("YYYY-MM-DD");
            getData();
            mePanel.dialog('close');
        }
    });
    $("#switchEquipOK").click(function() {
        var row = $('#lines_data').datagrid('getSelected');
        if (row) {
            lineId = row.lineId;
            equId = row.id;
            time = moment($('#ss').slider('getValue')).format("YYYY-MM-DD");
            getData();
            $('#switchEquip').dialog('close');
        } else {
            $.messager.alert({
                showSpeed: 100,
                title: '消息提示',
                msg: '请选择一条设备记录!',
                timeout: 3000
            });
        }
    });
});